package logic

import (
	"context"
	"doc-manage/models/mysql"

	"doc-manage/docrppc/docrppc"
	"doc-manage/docrppc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type UpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLogic {
	return &UpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateLogic) Update(in *docrppc.UpdateRequest) (*docrppc.UpdateResponse, error) {
	// todo: add your logic here and delete this line
	doc := mysql.Doc{
		Model: gorm.Model{},
		Name:  in.Name,
		From:  in.Form,
		Image: in.Image,
	}
	err := doc.DocUpdate(mysql.DB, in.DocsId)
	if err != nil {
		return nil, err
	}

	return &docrppc.UpdateResponse{
		DocsId: int64(doc.ID),
	}, nil
}
